package net.siufung.market.base.user.controller.regulation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.user.mapper.IUserMapper;
import net.siufung.market.base.user.mapper.entity.User;

import java.util.Objects;

/**
 * @author 陈建峰
 * @date 2023/2/7 1:30 PM
 * @since V1.0.0
 */
@AllArgsConstructor
public class UserPostRemoveRegulation extends AbstractRegulation<Integer> {

    private static final long serialVersionUID = 6247224959492540600L;

    private final IUserMapper userMapper;

    @Override
    public boolean isSatisfiedBy(Integer id) {
        if(Objects.isNull(id)){
            throw new BizServiceException("用户岗位主键为空！");
        }
        long userCount = userMapper.selectCount(
                new LambdaQueryWrapper<User>().eq(User::getPostId, id));
        if(userCount > 0){
            throw new BizServiceException("用户岗位在用户中已经占用！");
        }
        return Boolean.TRUE;
    }
}
